﻿package  
{
	import fl.controls.Button;
	import fl.controls.Label;
	import fl.controls.List;
	import fl.events.ComponentEvent;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.net.ObjectEncoding;
	import flash.net.Socket;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author kot
	 */
	public class MainChess extends MovieClip
	{
		public var connection : Connection;
		
		private var myId:int = 3371777;
		private var myPassword:String = "PASSWORD";
		private var onlineList: List; 
		private var challengeButton: Button;
		private var receiveChallengeButton:Button;
		
		public function MainChess() 
		{	
			connection = new Connection();
			connection.addGameEventListener(GameEvent.Register, onRegisterComplete);
			connection.addGameEventListener(GameEvent.Auth, onAuthComplete);
			connection.addGameEventListener(GameEvent.GetOnlinePlayers, onGotOnlineList);
			connection.addGameEventListener(GameEvent.InviteToPlay, onReceivedAnswerOnInvitation);
			connection.addGameEventListener(GameEvent.GotInvitation, onGotInvitation);
			

			connection.onConnected = onConnected;

			connection.connect();
			
			onlineList = new List();
			onlineList.x = 0;
			onlineList.y = 0;
			onlineList.width = 100;
			onlineList.height = 200;
			onlineList.addEventListener(MouseEvent.CLICK, setChallengeEnabled);
			addChild(onlineList);
			
			challengeButton = new Button();
			challengeButton.enabled = false;
			challengeButton.textField.text = "challenge";
			challengeButton.x = 0;
			challengeButton.y = stage.height - 100;
			challengeButton.width = 100;
			challengeButton.height = 100;
			challengeButton.addEventListener(ComponentEvent.BUTTON_DOWN, inviteToPlay);
			addChild(challengeButton);
			//onConnected -> onRegisterComplete -> onAuthComplete
		}
		
		private function setChallengeEnabled(e: MouseEvent) {
			challengeButton.enabled = true;
		}

		private function onConnected():void 
		{
			trace("Connected. Now registering...");
			register();	
		}
		private function onRegisterComplete(result: uint, respond: Object): void 
		{
			if (result != 0) {
				trace(getErrorDescription(result));
			}
			auth();
		}

		private function onAuthComplete(result:uint, respond: Object): void 
		{
			if (result != 0) {
				trace(getErrorDescription(result));
			}
			getOnline();
			
		}
		private function onGotOnlineList(result:uint, respond: Object): void {
			//respond = Array of unsigned ints
			if (result != 0) {
				trace(getErrorDescription(result));
				//return;
			}
			
			trace("Online opponents: ");
			for (var i : uint = 0; i < respond.length; i++) {
				trace(respond[i].id);
				var addObject: Object = new Object();
				addObject.label = respond[i].id;
				onlineList.addItem(addObject);
			}
			
		}
		
		private function onReceivedAnswerOnInvitation(result: uint, respond: Object): void {
			trace("Received answer for invitation. rc = "+ respond.rc);
		}
		
		private function onGotInvitation(result: uint, respond: Object): void {
			trace("Received invite from ["+ respond.oppId +"]. rc = "+ respond.rc);
		}
		private function register():void
		{
			connection.send(GameEvent.Register, { id:myId } );
		}
		
		private function auth():void
		{
			trace("Logging in.");
			connection.send(GameEvent.Auth, { id: myId, password: myPassword } );
		}
		
		private function getOnline() : void {
			trace("Getting online players");
			connection.send(GameEvent.GetOnlinePlayers, {});
		}
		
		private function inviteToPlay(e: ComponentEvent): void {
			var vk_id: uint = onlineList.selectedItem.label;
			trace("Challenge " + vk_id);
			trace(connection.connected);
			connection.send(GameEvent.InviteToPlay, {opp_id: vk_id} );
		}
		
		private function getErrorDescription(errorCode: uint): String
		{
			switch (errorCode) {
				case 0: return "Success";
				case 1: return "PlayerAlreadyExists";
				case 2: return "PlayerNotFound";
				case 3: return "PasswordIsIncorrect";
				case 4: return "PlayerNotAuthorized";
				case 5: return "IncorrectMessage";
				case 6: return "UnknownMethod";
				case 7: return "OutOfMemory";
				case 8: return "PlayerAlreadyAuthorized"; 
				
				default: return "Unknown error";
			}
		}
		
		
	}
}